package com.haibei.adapter.bean;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.common.bean.IBeanInterface;

public class City implements IBeanInterface{

	private String id;
	private String name;
	private ArrayList<City> subCity;
	private City parent;

	public City() {
		subCity = new ArrayList<City>();
	}

	public City(String id, String name) {
		subCity = new ArrayList<City>();
		this.id = id;
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public ArrayList<City> getSubCity() {
		return subCity;
	}

	public void setSubCity(ArrayList<City> subCity) {
		this.subCity = subCity;
	}

	public City getParent() {
		return parent;
	}

	public void addSubCity(City city) {
		this.subCity.add(city);
	}

	public void setParent(City parent) {
		this.parent = parent;
		parent.addSubCity(this);
	}

	public JSONObject toJson() throws JSONException {
		JSONObject jso = new JSONObject();
		jso.put("id", this.id);
		jso.put("name", this.name);

		if (null != parent) {
			JSONObject jsoP = new JSONObject();
			jsoP.put("id", parent.getId());
			jsoP.put("name", parent.getName());
			jso.put("parent", jsoP);
		}
		if (null != subCity && !subCity.isEmpty()) {

			JSONArray jsa = new JSONArray();
			for (int i = 0; i < subCity.size(); i++) {
				JSONObject subjso = new JSONObject();
				City sub = subCity.get(i);
				subjso.put("id", sub.getId());
				subjso.put("pid", this.id);
				subjso.put("name", sub.getName());
				jsa.put(subjso);
			}

			jso.put("sub", jsa);
		}
		return jso;
	}

}
